<script language="javascript" type="text/javascript">
var khachhang_id="";
/*--------------------------------------------------------------------------------------*/
function khachhang_a(){
	check_right("<?php echo $_SESSION["un"]?>","insert","khachhang",
	function(){

		var pten=encodeURI(trimStr($("#khachhang_a_table #ten").val()));
		var pdiachi=encodeURI($("#khachhang_a_table #diachi").val());
		var pdienthoai=encodeURI($("#khachhang_a_table #dienthoai").val());
		var pmsthue=encodeURI($("#khachhang_a_table #msthue").val());
		var pmota=encodeURI($("#khachhang_a_table #mota").val());
		var pchophep=$("#khachhang_a_table #chophep").val();
		
		$("#fkhachhang_add").dialog("option","disabled",true);
		$.ajax({
			url:"modules/khachhang/a.php",
			type:"POST",
			dataType:"xml",
			data:"ten="+pten+"&diachi="+pdiachi+"&dienthoai="+pdienthoai+"&msthue="+pmsthue+"&mota="+pmota+"&chophep="+pchophep,
			success:function(xml){
				var status=$(xml).find("status").text();
				var message=$(xml).find("message").text();
				msgbox(status,message);
			
				if(status=="ok") 
				{
					khachhang_id=$(xml).find("id").text();
					$("#fkhachhang_add").dialog("close");
				}
				else $("#fkhachhang_add").dialog("option","disabled",false);
			}
		});//ajax
	},function(){
			msgbox("fail","Không có quyền thêm danh mục khách hàng !");
	});
};
/*--------------------------------------------------------------------------------------*/
function khachhang_af(){
	check_right("<?php echo $_SESSION["un"];?>","insert","khachhang",function(){
		var fkhachhang_options={
			width:520,
			height:460,
			title:"Thêm khách hàng",
			modal:true,
			disabled:true,
			buttons:{
				"Thêm":khachhang_a,
				"Hủy":function(){$("#fkhachhang_add").dialog("close");}
			},
			focus:khachhang_af_ah,
			open:rebuildACL,
			close:function(){
				$("#fkhachhang_add").remove();
				$("#khachhang_flex").flexReload();
				khachhang_af_dh();
			}
		};
		
		$("body").append("<div id=\"fkhachhang_add\"></div>");
		
		$("#fkhachhang_add").dialog(fkhachhang_options);
		$("#fkhachhang_add").load("modules/khachhang/af.php",'',function(){
			$("#fkhachhang_add").dialog("option","disabled",false);
			$("#fkhachhang_add").dialog("option","position","center");
		});
	},function(){
		msgbox("fail","Không có quyền thêm danh mục khách hàng !");
	});
};
/*--------------------------------------------------------------------------------------*/
function khachhang_uf() {
	check_right("<?php echo $_SESSION["un"];?>","update","khachhang",
	function(){
		if(!$("#khachhang_flex .trSelected").attr("id"))
		{
			msgbox("fail","Phải chọn một mục để cập nhật !");
			return;
		}
		var sma=$("#khachhang_flex .trSelected").attr("id");
		sma=sma.substring(sma.lastIndexOf("row")+3);
		$.ajax({
			url:"modules/khachhang/s.php",
			type:"POST",
			dataType:"xml",
			data:"id="+sma,
			success:function(xml){
				
				var k=$(xml).find("row");
				var fkhachhang_options={
							width:520,
							height:460,
							title:"Cập nhật khách hàng",
							modal:true,
							buttons:{
								"Cập nhật":khachhang_u,
								"Hủy":function(){$("#fkhachhangupdate").dialog("close");}
							},
							disabled:true,
							focus:khachhang_uf_ah,
							open:rebuildACL,
							close:function(){
								$("#fkhachhangupdate").remove();
								khachhang_id=sma;
								$("#khachhang_flex").flexReload();
								khachhang_uf_dh();
							}
						};

						$("body").append("<div id=\"fkhachhangupdate\"></div>");
						$("#fkhachhangupdate").dialog(fkhachhang_options);
						$("#fkhachhangupdate").load("modules/khachhang/uf.php","",function(){
							$("#khachhang_u_table #id").attr("value",sma);
							if(k[0].childNodes[1].text)
								$("#khachhang_u_table #ten").attr("value",k[0].childNodes[1].text);
							else 
								$("#khachhang_u_table #ten").attr("value",k[0].childNodes[1].textContent);

							if(k[0].childNodes[2].text)
								$("#khachhang_u_table #diachi").attr("value",k[0].childNodes[2].text);
							else 
								$("#khachhang_u_table #diachi").attr("value",k[0].childNodes[2].textContent);

							if(k[0].childNodes[3].text)
								$("#khachhang_u_table #dienthoai").attr("value",k[0].childNodes[3].text);
							else 
								$("#khachhang_u_table #dienthoai").attr("value",k[0].childNodes[3].textContent);

							if(k[0].childNodes[4].text)
								$("#khachhang_u_table #msthue").attr("value",k[0].childNodes[4].text);
							else 
								$("#khachhang_u_table #msthue").attr("value",k[0].childNodes[4].textContent);

							if(k[0].childNodes[5].text)
								$("#khachhang_u_table #mota").attr("value",k[0].childNodes[5].text);
							else 
								$("#khachhang_u_table #mota").attr("value",k[0].childNodes[5].textContent);

							if(k[0].childNodes[6].text)
								$("#khachhang_u_table #chophep").attr("value",k[0].childNodes[6].text);
							else 
								$("#khachhang_u_table #chophep").attr("value",k[0].childNodes[6].textContent);

							if(k[0].childNodes[7].text)
								$("#khachhang_u_table #ngaytao").attr("value",k[0].childNodes[7].text);
							else 
								$("#khachhang_u_table #ngaytao").attr("value",k[0].childNodes[7].textContent);

							if(k[0].childNodes[8].text)
								$("#khachhang_u_table #un").attr("value",k[0].childNodes[8].text);
							else 
								$("#khachhang_u_table #un").attr("value",k[0].childNodes[8].textContent);
							
							$("#fkhachhangupdate").dialog("option","disabled",false);
							$("#fkhachhangupdate").dialog("option","position","center");
						});
					}
		});//ajax
	},function(){
		msgbox("fail","Không có quyền thêm danh mục khách hàng !");
	});
};
/*--------------------------------------------------------------------------------------*/
function khachhang_u(){
	check_right("<?php echo $_SESSION["un"]?>","update","khachhang",
	function(){
		var pid=$("#khachhang_u_table #id").val();
		var pten=encodeURI(trimStr($("#khachhang_u_table #ten").val()));
		var pdiachi=encodeURI($("#khachhang_u_table #diachi").val());
		var pdienthoai=encodeURI($("#khachhang_u_table #dienthoai").val());
		var pmsthue=encodeURI($("#khachhang_u_table #msthue").val());
		var pmota=encodeURI($("#khachhang_u_table #mota").val());
		var pchophep=$("#khachhang_u_table #chophep").val();

		$("#fkhachhangupdate").dialog("option","disabled",true);
		$.ajax({
			url:'modules/khachhang/u.php',
			type:"POST",
			dataType:"xml",
			data:"id="+pid+"&ten="+pten+"&diachi="+pdiachi+"&dienthoai="+pdienthoai+"&msthue="+pmsthue+"&mota="+pmota+"&chophep="+pchophep,
			success:function(xml){
				var status=$(xml).find("status").text();
				var message=$(xml).find("message").text();
				msgbox(status,message);
				if(status=="ok") $("#fkhachhangupdate").dialog("close");
				else $("#fkhachhangupdate").dialog("option","disabled",false);
			}
		});
	},
	function(){
		msgbox("fail","Không có quyền thêm danh mục khách hàng !");
	});
};
/*--------------------------------------------------------------------------------------*/
function khachhang_df(){
	check_right("<?php echo $_SESSION["un"];?>","delete","khachhang",
	function(){
		if(!$("#khachhang_flex .trSelected").attr("id"))
		{
			msgbox("fail","Phải chọn một mục để  xoá !");	
			return;
		}
		var sma=$("#khachhang_flex .trSelected").attr("id");
		sma = sma.substring(sma.lastIndexOf("row")+3);
		$.confirmDialog({
				msg:"Có chắc chắn xoá khách hàng "+sma+" không ?",
				yes:"Có",
				no:"Không",
				title:"Xác nhận xoá"
		},
		function(){
			$("#fkhachhang").dialog("option","disabled",true);
			$.ajax({
				url:"modules/khachhang/d.php",
				type:"POST",
				dataType:"xml",
				data:"id="+sma,
				success:function(xml){
					var status=$(xml).find("status").text();
					var message=$(xml).find("message").text();
					
					msgbox(status,message);
					if(status=="ok") {
						khachhang_id="";
						$("#khachhang_flex").flexReload();
					}
					$("#fkhachhang").dialog("option","disabled",false);
					khachhang_ah();
				}
			});//ajax
		},function(){
			khachhang_ah();
		});//confirm
	},
	function(){
		msgbox("fail","Không có quyền xóa khách hàng !");
	});
};
/*--------------------------------------------------------------------------------------*/
function khachhang_af_hk(e)
{
	var code=(e.keyCode ? e.keyCode : e.which);
	if(code==13)
	{
		$("#fkhachhang_add").dialog("option","buttons")["Thêm"]();
	}
	else if(code==27)
	{
		$("#fkhachhang_add").dialog("close");
	}
	e.stopPropagation();
	e.preventDefault();
}
/*--------------------------------------------------------------------------------------*/
function khachhang_uf_hk(e)
{
	var code=(e.keyCode ? e.keyCode : e.which);
	if(code==13)
	{
		$("#fkhachhang_update").dialog("option","buttons")["Cập nhật"]();
	}
	else if(code==27)
	{
		$("#fkhachhang_update").dialog("close");
	}
	e.stopPropagation();
	e.preventDefault();
}
/*--------------------------------------------------------------------------------------*/
function khachhang_af_ah()
{
	$(document).off("keydown");
	$(document).on("keydown",khachhang_af_hk);
}
/*--------------------------------------------------------------------------------------*/
function khachhang_af_dh()
{
	$(document).off("keydown");
	$(document).on("keydown",khachhang_hk);
}
/*--------------------------------------------------------------------------------------*/
function khachhang_uf_ah()
{
	$(document).off("keydown");
	$(document).on("keydown",khachhang_uf_hk);
}
/*--------------------------------------------------------------------------------------*/
function khachhang_uf_dh()
{
	$(document).off("keydown");
	$(document).on("keydown",khachhang_hk);
}
/*--------------------------------------------------------------------------------------*/
function khachhang_hk(e)
{
	var code=(e.keyCode ? e.keyCode : e.which);
	//Down
	if(code==38)
	{
		if($("#khachhang_flex tr.trSelected").is("#khachhang_flex tr:first")) return;
		var id=$("#khachhang_flex tr.trSelected").prev().attr("id");
		$("#khachhang_flex tr.trSelected").removeClass("trSelected");
		$("#khachhang_flex #"+id).addClass("trSelected");
	}
	// Up
	else if(code==40)
	{
		if($("#khachhang_flex tr.trSelected").is("#khachhang_flex tr:last")) return;
		var id=$("#khachhang_flex tr.trSelected").next().attr("id");
		$("#khachhang_flex tr.trSelected").removeClass("trSelected");
		$("#khachhang_flex #"+id).addClass("trSelected");
	}
	//Enter
	else if(code==13)
	{
		$("#fkhachhang").dialog("option", "buttons")["Sửa"]();
	}
	// Xóa
	else if(code==46)
	{
		$("#fkhachhang").dialog("option", "buttons")["Xoá"]();
	}
	// +
	else if(code==107)
	{
		$("#fkhachhang").dialog("option", "buttons")["Thêm"]();
	}
	e.stopPropagation();
	e.preventDefault();
}
/*--------------------------------------------------------------------------------------*/
function khachhang_ah()
{
	$(document).off("keydown");
	$(document).on("keydown",khachhang_hk);
	if(event!=null) if(typeof(event)!="undefined")
	{
		event.stopPropagation();
		event.preventDefault();
	}
}
/*--------------------------------------------------------------------------------------*/
function khachhang_dh()
{
	$(document).off("keydown");
}
/*--------------------------------------------------------------------------------------*/
function khachhang(){
	check_right("<?php echo $_SESSION["un"]?>","select","khachhang",
	function(){
		if($("#fkhachhang").is(":data(dialog)")) 
		{
			msgbox("fail","Đã có cửa sổ Khách hàng được mở !");
			return;
		}
		$("body").append("<div id=\"fkhachhang\"></div>");
		
		var fkhachhang_options={
			width:960,
			height:550,
			resizable:false,
			disabled:true,
			title:"Danh mục khách hàng",
			buttons:{
				"Thêm":khachhang_af,
				"Sửa":khachhang_uf,
				"Xoá":khachhang_df
			},
			focus:khachhang_ah,
			close:function(){
				$("#fkhachhang").remove();
				khachhang_dh();
			}
		};
		$("#fkhachhang").dialog(fkhachhang_options);
		$("#fkhachhang").load("modules/khachhang/g.php","",function(){
			$("#fkhachhang").dialog("option","disabled",false);
		});
	},function(){
			msgbox("fail","Không có quyền xem danh mục khách hàng !");
	});
};//showadv
</script>
